package com.laf.manager.controller;

import com.laf.manager.dto.ParkingRule;
import com.laf.manager.dto.ReceiveCouponInfo;
import com.laf.manager.dto.Reward;
import com.laf.manager.enums.CouponType;
import com.laf.manager.enums.RewardCategory;
import com.laf.manager.schedule.ScheduleConfig;
import com.laf.manager.service.ParkingRuleService;
import com.laf.manager.service.PointsDeductionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ygl
 * @create 2018-12-27
 * @DESCRIPTION
 * 停车抵扣规则
 **/
@Controller
@Slf4j
public class ParkingRuleController {

    @Autowired
    private ParkingRuleService parkingRuleService;

    @Autowired
    private PointsDeductionService pointsDeductionService;

    @Autowired
    private ScheduleConfig scheduleConfig;

    @GetMapping("/parkingRule")
    public ModelAndView getParkingRule() {
        ModelAndView model = new ModelAndView();
        model.setViewName("parkingRule");
        ParkingRule one = parkingRuleService.findOne();
        model.getModel().put("model",one);
        return model;
    }

    @PostMapping("/parkingRule")
    public String setPointsRule(ParkingRule model) {
        Integer id = model.getId();
        if (id == null) {
            parkingRuleService.insert(model.getPointsPerYuan(),model.getPointsPerYuan(),model.getFrozenTime());
        } else {
            parkingRuleService.update(model);
        }
        ParkingRule one = parkingRuleService.findOne();
        if (one != null && one.getFrozenTime() != null) {
            // 启动定时任务
            pointsDeductionService.startTask(scheduleConfig,one.getFrozenTime());
        }

        return "redirect:parkingRule";
    }
}
